#!/bin/bash
# use this script to record each disk, ethernet card speed per second
# and average speed for total

#initiallization

file=/tmp/${RANDOM}CPU

#rm ././*.dat -f
cout=0

if [[ $# != 1 ]]; then
    echo $#
    echo "Usage:  $0 [acctime] "
    exit -1
fi

acctime=$1

while : ; do
    let "cout=$cout+1"

    sar -u 1 1 > $file

    user=`cat $file | head -n 4 | tail -1 | awk '{print $4}'`
    echo "$cout $user" >> ./cpu_user.dat

    sys=`cat $file | head -n 4 | tail -1 | awk '{print $6}'`
    echo "$cout $sys" >> ./cpu_sys.dat

    iowait=`cat $file | head -n 4 | tail -1 | awk '{print $7}'`
    echo "$cout $iowait" >> ./cpu_iowait.dat

    idle=`cat $file | head -n 4 | tail -1 | awk '{print $9}'`
    echo "$cout $idle" >> ./cpu_idle.dat

# time control
    if [[ $cout == $acctime ]] ; then
        break;
    fi
done

